import random
from django.conf import settings
from django.http.response import JsonResponse
from django.shortcuts import render
from django.shortcuts import HttpResponse

from utils.tencent.sms import send_sms_single
from web.Forms import account


# Tencent发送短信
def sms_msg(request):
    """
    发送短信
    ?tpl = register ---15161
    ?tpl = login ---15161
    """
    tpl = request.GET.get('tpl')
    templates_id = settings.TENCENT_SMS_TEMPLATES.get(tpl)
    if not templates_id:
        return HttpResponse('请输入正确的模板id')
    code = random.randrange(1000, 9999)
    res = send_sms_single(19803630852, templates_id, [code])
    if res["result"] == 0:
        return HttpResponse('成功')
    return HttpResponse(res['errmsg'])


# 注册测试
def Register(request):
    dic = {
        'msg': 'xxx'
    }
    if request.method == "GET":
        form = account.register_form()
        return render(request, 'app01/register.html', locals())
    print('我是POST:', request.POST)
    print('我是body:', request.body)
    return JsonResponse(dic)
